// 我的页面
<template>
  <div>
    <el-row class="el-row">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-input
            class="ipt"
            placeholder="请输入内容"
            v-model="inputVal"
            clearable
          >
          </el-input>
          <el-button type="primary" @click="search">筛选</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light btn">
          <el-button type="primary">批量删除</el-button>
          <el-button type="primary" @click="adduser">添加</el-button>
        </div>
      </el-col>
    </el-row>
    <template>
      <el-table
        :header-cell-style="{ 'text-align': 'center' }"
        border
        :data="listArr"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="80"> </el-table-column>
        <el-table-column prop="age" label="年龄" width="80"> </el-table-column>
        <el-table-column prop="major" label="专业" width="100">
        </el-table-column>
        <el-table-column prop="graduateTime" label="毕业日期" width="180">
        </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column prop="pay" label="缴费" width="80"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="listArr.size"
      :current-page="listArr.page"
    >
    </el-pagination>
    <!-- 编辑对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <!-- input框 -->
      <div>
        <p>
          <el-input v-model="currentItem.name"></el-input>
        </p>
        <p>
          <el-input v-model="currentItem.age"></el-input>
        </p>
        <p>
          <el-input v-model="currentItem.major"></el-input>
        </p>
        <p>
          <el-input v-model="currentItem.pay"></el-input>
        </p>
        <p>
          <el-input v-model="currentItem.graduateTime"></el-input>
        </p>
        <p>
          <el-input v-model="currentItem.address"></el-input>
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { userApi, deleteuser, adduser, edituser } from '../utile/https'
export default {
  data () {
    return {
      inputVal: '',
      page: 1,      //当前页
      size: 10,    // 、、每页展示10条数据
      total: 1,     //数据总数
      listArr: [],  //渲染的数组
      dialogVisible: false,     //编辑对话框是否显示
      currentItem: {},      //当前用户信息
      btnStatus: 'add'        //按钮状态
    }
  },


  //   做到编辑功能
  created () {
    this.getlist()
  },
  methods: {
    //编辑按钮
    handleEdit (index, row) {
      //编辑按钮
      this.btnStatus = "edit";
      this.dialogVisible = true
      this.currentItem = row
      console.log(index, row);

    },
    // 编辑里面的确定按钮
    editSure () {
      this.dialogVisible = false
      if (this.btnStatus == 'edit') {
        edituser(this.currentItem).then(res => {
          if (res.code == 200) {
            this.getlist();
          }
        })
      } else {
        console.log(11, this.currentItem);
        adduser(this.currentItem).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.getlist();
          }
        });
      }

    },
    //添加按钮
    adduser () {
      this.btnStatus = "add";
      // 显示模态框
      this.dialogVisible = true;
      this.currentItem = {};
    },
    //删除按钮
    handleDelete (index, row) {
      //   console.log(index, row);
      deleteuser({ id: row.id }).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        }
      })
      this.getlist()
    },
    search () {
      this.getlist()
    },
    getlist () {
      userApi({
        searchvalue: this.inputVal,
        page: this.page,
        size: this.size
      }).then((res) => {
        this.listArr = res.data
        this.total = res.totalpage
        // console.log(this.listArr);
      })

    },
    //修改page发生变化
    handleCurrentChange (val) {
      this.page = val
      this.getlist()
    },


  }
}
</script>

<style lang="less" scoped>
.bg-purple {
  margin-left: 20px;
  display: flex;
  //   justify-content: space-between;
  .ipt {
    width: 200px;
    margin-right: 20px;
  }
}
.btn {
  margin-right: 80px;
  float: right;
}
.el-row {
  background: rgb(255, 255, 255);
  height: 100px;
  //   line-height: 100px;
  padding: 30px;
  margin-top: 30px;
}
.infobtn {
  display: flex;
  justify-content: space-evenly;
}
</style>
